package com.hmall.order.utils;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;

public class RabbitMQConfig {
    public static final String REFUND_QUEUE = "refund_queue";
    public static final String REFUND_ERROR_QUEUE = "refund_error_queue";


    /**
     * ttl交换机
     */
    public static final String TTL_EXCHANGE = "springboot_ttl_exchange";
    /**
     * ttl队列
     */
    public static final String TTL_QUEUE = "springboot_ttl_queue";
    /**
     * 死信交换机
     */
    public static final String DLX_EXCHANGE = "springboot_dlx_exchange";
    /**
     * 死信队列
     */
    public static final String DLX_QUEUE = "springboot_dlx_queue";


    // 基于插件的交换机和队列
    public static final String PLUGINS_QUEUE = "plugins_queue";

    public static final String PLUGINS_EXCHANGE = "plugins_exchange";

    /**
     * 声明ttl交换机
     *
     * @return ttl交换机
     */
    @Bean
    public Exchange ttlExchange() {
        return ExchangeBuilder.topicExchange(TTL_EXCHANGE).build();
    }
    /**
     * 声明死信交换机
     *
     * @return 死信交换机
     */
    @Bean
    public Exchange dlxExchange() {
        return ExchangeBuilder.topicExchange(DLX_EXCHANGE).build();
    }
    /**
     * 声明死信队列
     *
     * @return 死信队列
     */
    @Bean
    public Queue dlxQueue() {
        return QueueBuilder.durable(DLX_QUEUE).build();
    }
    /**
     * 声明ttl队列
     *
     * @return 死信队列
     */
    @Bean
    public Queue ttlQueue() {
        return QueueBuilder.durable(TTL_QUEUE)
                //该队列中的消息过期时间，单位毫秒
                .ttl(60000)
                //绑定死信交换机
                .deadLetterExchange(DLX_EXCHANGE)
                //设置死信交换机routingKey
                .deadLetterRoutingKey("dlx.haha")
                //设置队列最大消息数量为100
                .maxLength(100)
                .build();
    }
    /**
     * 绑定ttl队列到ttl交换机
     *
     * @param queue    ttl队列
     * @param exchange ttl交换机
     */
    @Bean
    public Binding bindingTtl(@Qualifier("ttlQueue") Queue queue,
                              @Qualifier("ttlExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("ttl.#").noargs();
    }
    /**
     * 绑定死信队列到死信交换机
     *
     * @param queue    死信队列
     * @param exchange 死信交换机
     */
    @Bean
    public Binding bindingDlx(@Qualifier("dlxQueue") Queue queue,
                              @Qualifier("dlxExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("dlx.#").noargs();
    }

}
